import { getToken, setToken, removeToken } from '@/utils/auth.js'
import { login, getInfo, getimg } from '@/api/user'
import { resetRouter } from '@/router/index'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {}
  },
  mutations: {
    updatalist(state, token) {
      state.token = token
      setToken(token) // 持久化token
    },
    getinfofo(state, info) {
      state.userinfo = info
    },
    removetoken(state) {
      state.token = ''
      removeToken()
    },
    removeinfo(state) {
      state.userinfo = {}
      removeToken()
    }
  },
  actions: {
    async loginlist(context, formlist) {
      const res = await login(formlist)
      console.log(res)
      context.commit('updatalist', res.data)
    },
    async  getlist(centext) {
      const res = await getInfo()
      const red = await getimg(res.data.userId)
      console.log(red)
      console.log(res.data)
      centext.commit('getinfofo', { ...res.data, ...red.data })
      return res.data.roles.menus
    },
    removelist(context) {
      context.commit('removetoken')
      context.commit('removeinfo')
      resetRouter()
    }
  }
}

